package cn.xumaoyang.basic.service.impl;

import cn.xumaoyang.basic.util.PageList;
import cn.xumaoyang.basic.mapper.Basemapper;
import cn.xumaoyang.basic.query.BaseQuery;
import cn.xumaoyang.basic.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true , propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    //这里必须要加泛型。
    @Autowired
    private Basemapper<T> basemapper;
    
    //添加
    @Override
    @Transactional
    public void add(T t) {
        basemapper.add(t);
    }

    //修改
    @Override
    @Transactional
    public void update(T t) {
        basemapper.update(t);
    }

    //删除
    @Override
    @Transactional
    public void delete(Long id) {
        basemapper.delete(id);
    }

    //查询单个
    @Override
    public T loadById(Long id) {
        return basemapper.loadById(id);
    }

    //查询所有。
    @Override
    public List<T> loadAll() {
        return basemapper.loadAll();
    }

    //分页查询。
    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //查询总数量。
        Long total = basemapper.queryCount(query);
        //查询当前设置的数量的数据。
        List<T> rows = basemapper.queryData(query);
        //把数据返回回去实现分页查询，和查询总数量和全部数据。
        return new PageList<>(total,rows);
    }

    //批量删除
    @Override
    @Transactional
    public void deletes(List<Long> ids) {
        basemapper.deletes(ids);
    }
}
